﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using SuperQuiz.Common;
using SuperQuiz.Logic;
using SuperQuiz.Model;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace SuperQuiz.View
{
    public sealed partial class QuizResultsControl : LayoutAwarePage
    {
        public QuizResultsControl()
        {
            this.InitializeComponent();
        }

        private void SaveScoreClick(object sender, RoutedEventArgs e)
        {
            SaveScore();
        }

        private async Task SaveScore()
        {
            var quizResults = (QuizResults)DataContext;
            var quizScore = new QuizScore()
            {
                DatePlayed = quizResults.DatePlayed,
                FinalScore = quizResults.FinalScore,
                PlayerName = quizResults.PlayerName,
                QuizUniqueId = quizResults.Quiz.UniqueId,
            };
            quizResults.ScoreSaved = true;
            await QuizApplication.Instance.SaveScore(quizScore);
        }

        private void SaveAndShareScoreClick(object sender, RoutedEventArgs e)
        {
            SaveScore();
            ShareScore();
        }

        private void ShareScore()
        {
            ApplicationView.TryUnsnap();
            DataTransferManager.ShowShareUI();
        }

        private void ShareScoreClick(object sender, RoutedEventArgs e)
        {
            ShareScore();
        }
    }
}
